//
//  EditViewController.m
//  Meroad
//
//  Created by 郭孝通 on 2019/4/19.
//  Copyright © 2019 郭孝通. All rights reserved.
//

#import "EditViewController.h"
#import "AppDelegate.h"

@interface EditViewController ()

@property (nonatomic, copy) NSMutableArray *textArray;
@property (nonatomic, copy) NSMutableArray *informationArray;

- (IBAction)return:(id)sender;

@end

@implementation EditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    //tableView中依次显示如下的image和text
    _textArray = [NSMutableArray array];
    _informationArray = [NSMutableArray array];
    
    NSString *str = [NSString stringWithFormat:@"昵称"];
    [_textArray addObject:str];
    
    str = [NSString stringWithFormat:@"性别"];
    [_textArray addObject:str];
    
    str = [NSString stringWithFormat:@"生日"];
    [_textArray addObject:str];
    
    str = [NSString stringWithFormat:@"年龄"];
    [_textArray addObject:str];
    
    str = [NSString stringWithFormat:@"我的邮箱"];
    [_textArray addObject:str];
    
    str = [NSString stringWithFormat:@"我的电话"];
    [_textArray addObject:str];
    
    str = [NSString stringWithFormat:@"职业"];
    [_textArray addObject:str];
    
    str = [NSString stringWithFormat:@"所在地"];
    [_textArray addObject:str];
    
    str = [NSString stringWithFormat:@"故乡"];
    [_textArray addObject:str];
    
}

- (void)viewWillAppear:(BOOL)animated{
    _informationArray = [self removeTheWholeArray:_informationArray];
    NSString *str = [NSString stringWithFormat:@"%@",self.userModel.userName];
    [_informationArray addObject:str];
    
    str = [NSString stringWithFormat:@"%@",self.userModel.userGender];
    [_informationArray addObject:str];
    
    str = [NSString stringWithFormat:@"%@",self.userModel.userBirth];
    [_informationArray addObject:str];
    
    str = [NSString stringWithFormat:@"%@",self.userModel.userAge];
    [_informationArray addObject:str];
    
    str = [NSString stringWithFormat:@"%@",self.userModel.userEmail];
    [_informationArray addObject:str];
    
    str = [NSString stringWithFormat:@"%@",self.userModel.userTel];
    [_informationArray addObject:str];
    
    str = [NSString stringWithFormat:@"%@",self.userModel.userOccupation];
    [_informationArray addObject:str];
    
    str = [NSString stringWithFormat:@"%@",self.userModel.userPlace];
    [_informationArray addObject:str];
    
    str = [NSString stringWithFormat:@"%@",self.userModel.userHome];
    [_informationArray addObject:str];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark delegate

//返回多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

//每一组返回多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0)  return 4;
    if(section == 1) return 5;
    return 0;
}

//每一行显示数据
//UITableViewCell: 每一行显示的格子
//indexPath: 代表唯一的一行
//indexPath.section: 获取对应的组号
//indexPath.row: 获取行号
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //第几个cell
    NSInteger num = indexPath.section * 4 + indexPath.row;
    NSLog(@"num: %ld",num);
    //建立标志符字符串
    static NSString *ID = @"Cell";
    //让cell从缓存池中出来
    //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    UITableViewCell *cell;
    //[tableView.visibleCells objectAtIndex:num];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                      reuseIdentifier:ID];
        [cell.imageView setFrame:CGRectMake(0, 0, 5, 0)];
        [cell.textLabel setText:[_textArray objectAtIndex:num]];
        
        [cell.textLabel setFont:[UIFont systemFontOfSize:20]];
        
        if(num < 9){
            [cell.detailTextLabel setText:[_informationArray objectAtIndex:num]];
        }
    }else{
        if(num < 9){
            [cell.detailTextLabel setText:[_informationArray objectAtIndex:num]];
        }
    }
    
    [cell setAccessoryType:UITableViewCellAccessoryNone];
    cell.userInteractionEnabled = YES;
    
    return cell;
}

//返回头部文字
-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
    return NULL;
}

//返回尾部文字
-(NSString*)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section{
    return NULL;
}

//设置某一行cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}


//section头部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 33;//section头部高度
}

//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return NULL;
}

//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if(section == 1){
        return 240;
    }
    return 10;
}

//section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return NULL;
}

//设置cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //当手指离开某行时，就让某行的选中状态消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSInteger num = indexPath.section * 4 + indexPath.row;
    
    UIAlertController *alert;
    
    
    switch (num) {
        case 0:
            alert = [UIAlertController alertControllerWithTitle:@"昵称" message:@"输入你的昵称" preferredStyle:UIAlertControllerStyleAlert];
            break;
        case 1:
        {
            alert = [UIAlertController alertControllerWithTitle:@"性别" message:@"请选择" preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                self.userModel.userGender = [NSString stringWithFormat:@"男"];
                
                [self.informationArray replaceObjectAtIndex:num withObject:self.usermodel.userGender];
                
                //刷新
                [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
                //[tableView reloadData];
                
                
            }]];
            [alert addAction:[UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                self.userModel.userGender = [NSString stringWithFormat:@"女"];
                
                [self.informationArray replaceObjectAtIndex:num withObject:self.usermodel.userGender];
                
                //刷新
                [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
                //[tableView reloadData];
                
            }]];
            break;
        }
        case 2:
            alert = [UIAlertController alertControllerWithTitle:@"生日" message:@"请输入您的生日" preferredStyle:UIAlertControllerStyleAlert];
            break;
        case 3:
            alert = [UIAlertController alertControllerWithTitle:@"年龄" message:@"请输入您的年龄" preferredStyle:UIAlertControllerStyleAlert];
            break;
        case 4:
            alert = [UIAlertController alertControllerWithTitle:@"我的邮箱" message:@"请输入您的邮箱" preferredStyle:UIAlertControllerStyleAlert];
            break;
        case 5:
            alert = [UIAlertController alertControllerWithTitle:@"我的电话" message:@"请输入您的电话" preferredStyle:UIAlertControllerStyleAlert];
            break;
        case 6:
            alert = [UIAlertController alertControllerWithTitle:@"我的职业" message:@"请输入您的职业" preferredStyle:UIAlertControllerStyleAlert];
            break;
        case 7:
            alert = [UIAlertController alertControllerWithTitle:@"我的所在地" message:@"请输入您的所在地" preferredStyle:UIAlertControllerStyleAlert];
            break;
        case 8:
            alert = [UIAlertController alertControllerWithTitle:@"我的故乡" message:@"请输入您的故乡" preferredStyle:UIAlertControllerStyleAlert];
            break;
        default:
            break;
    }
    
    if(num != 1){
        [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            switch (num) {
                default:
                    textField.keyboardType = UIKeyboardTypeEmailAddress;
                    break;
            }
            textField.placeholder = [self.informationArray objectAtIndex:num];
            
        }];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *temp = alert.textFields.firstObject.text;
            
            switch (num) {
                case 0:
                {
                    self.userModel.userName = temp;
                    
                    [self.informationArray replaceObjectAtIndex:0 withObject:temp];
                    break;
                }
                case 2:
                {
                    self.userModel.userBirth = temp;
                    
                    [self.informationArray replaceObjectAtIndex:2 withObject:temp];
                    break;
                }
                case 3:
                {
                    self.userModel.userAge = temp;
                    
                    [self.informationArray replaceObjectAtIndex:3 withObject:temp];
                    break;
                }
                case 4:
                {
                    self.userModel.userEmail = temp;
                    
                    [self.informationArray replaceObjectAtIndex:4 withObject:temp];
                    break;
                }
                case 5:
                {
                    self.userModel.userTel = temp;
                    
                    [self.informationArray replaceObjectAtIndex:5 withObject:temp];
                    break;
                }
                case 6:
                {
                    self.userModel.userOccupation = temp;
                    
                    [self.informationArray replaceObjectAtIndex:6 withObject:temp];
                    break;
                }
                case 7:
                {
                    self.userModel.userPlace = temp;
                    
                    [self.informationArray replaceObjectAtIndex:7 withObject:temp];
                    break;
                }
                case 8:
                {
                    self.userModel.userHome = temp;
                    
                    [self.informationArray replaceObjectAtIndex:8 withObject:temp];
                    break;
                }
                default:
                    break;
            }
            
//            //刷新
//            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
            [tableView reloadData];
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    }
    
    
    //弹出提示框；
    [self presentViewController:alert animated:true completion:nil];
    
}

#pragma mark others

-(NSMutableArray *)removeTheWholeArray:(NSMutableArray *)array{
    if([array count]){
        [array removeAllObjects];
        return array;
    }else{
        return array;
    }
}

#pragma mark 重写get方法(懒加载)

- (UserModel *)userModel{
    if (_usermodel == nil) {
        AppDelegate *_rootDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        _usermodel = _rootDelegate.userModel;
    }
    return _usermodel;
}

#pragma mark action

- (IBAction)return:(id)sender {
    [self dismissViewControllerAnimated:YES
                             completion:nil];
}

@end
